# -*- coding: utf-8 -*-
 
import uuid
import hashlib
from base import UserCase, Runner, TestSuite
import os, sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from app.service.multipart import encode_multipart
from app.lib.captcha import captcha


class Case(UserCase):

    def test_tool_check_username(self):
        username = self.randchar(10)
        res = self.c.open('/api/tool/check-username', {'username': username})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['exists'], False)

        p = {
            'name': self.randhan(3),
            'type': 'admin',
            'password': hashlib.md5(self.randchar(6)).hexdigest(),
            'username': self.randchar(8),
        }
        res = self.c.open('/api/user/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/tool/check-username', {'username': p['username']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['exists'], True)

    def test_tool_upload(self):
        code, buff = captcha.gen()
        boundary = uuid.uuid4().hex
        p = {
            'data': buff,
        }
        body = encode_multipart(boundary, p)
        header = {
            'Content-Type': 'multipart/form-data; boundary={}'.format(boundary),
        }
        res = self.c.open('/api/tool/upyun', params=body, headers=header)
        self.assertEqual(res['code'], 0)


test_list = [
    'test_tool_check_username',
    'test_tool_upload',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



